package servlet;

import dao.PersonDAO;
import entity.Page;
import entity.PersonBean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import utils.StrFilter;

import java.io.IOException;
import java.util.List;
@WebServlet("/group/people/personList")
public class PersonListServlet extends HttpServlet {
    public PersonListServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession(); // 获取会话
        // 获取先前的查询条件
        String previousNumber = (String) session.getAttribute("previousNumber");
        String previousName = (String) session.getAttribute("previousName");
        String previousDegree = (String) session.getAttribute("previousDegree");
        String previousPosition = (String) session.getAttribute("previousPosition");
        String previousTel = (String) session.getAttribute("previousTel");

        String person_number = StrFilter.filter(req.getParameter("person_number"));
        String person_name = StrFilter.filter(req.getParameter("name"));
        String degree = StrFilter.filter(req.getParameter("degree"));
        String person_position = StrFilter.filter(req.getParameter("person_position"));
        String tel = StrFilter.filter(req.getParameter("tel"));

        Page personPage = (Page) session.getAttribute("personPage");
        // 检查查询条件是否已更新
        if (!person_number.equals(previousNumber) || !person_name.equals(previousName) || !degree.equals(previousDegree)|| !person_position.equals(previousPosition)|| !tel.equals(previousTel)) {
            // 重新初始化页数
            personPage=null;
        }
        if (personPage == null  ) {
            personPage = new Page();
            session.setAttribute("personPage", personPage);
        }

        //分页逻辑
        if(req.getParameter("currentPage") != null) {
            personPage.setCurrentPage(Integer.parseInt(req.getParameter("currentPage"))) ;
        }


        PersonDAO dao = new PersonDAO();
        List<PersonBean> personList = dao.findPerson(person_number,person_name,degree,person_position,tel,personPage);

        // 保存当前查询条件以供下次检查
        session.setAttribute("previousNumber", person_number);
        session.setAttribute("previousName", person_name);
        session.setAttribute("previousDegree", degree);
        session.setAttribute("previousPosition", person_position);
        session.setAttribute("previousTel", tel);


        req.setAttribute("personList", personList);
        req.setAttribute("person_number", person_number);
        req.setAttribute("person_name", person_name);
        req.setAttribute("degree", degree);
        req.setAttribute("person_position", person_position);
        req.setAttribute("tel", tel);

        req.setAttribute("personPage",personPage);

        req.getRequestDispatcher("person_list.jsp").forward(req, resp);
    }
}
